package controller

import (
	"digital/server/common"
	"digital/server/model"
	"digital/server/response"
	"fmt"
	mssql "github.com/denisenkom/go-mssqldb"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
	"time"
)

func CreateAct(ctx *gin.Context) {
	uid := ctx.PostForm("uid") //申请创建活动的用户id
	userid, _ := strconv.Atoi(uid)
	//aid := ctx.PostForm("aid")
	//actid, _ := strconv.Atoi(aid)

	AName := ctx.PostForm("AName")
	cid := ctx.PostForm("cid")
	clubid, _ := strconv.Atoi(cid)
	Time := ctx.PostForm("CreateTime") //申请创建社团时间
	ApplyCreateTime, _ := time.Parse("2006-01-02 15:04:05", Time)

	ActTitle := AName + "创建活动申请"

	startTime1 := ctx.PostForm("startTime")
	startTime, _ := time.Parse("2006-01-02 15:04:05", startTime1)
	endTime1 := ctx.PostForm("endTime")
	endTime, _ := time.Parse("2006-01-02 15:04:05", endTime1)

	CampusStr := ctx.PostForm("campus")
	Campus, _ := strconv.Atoi(CampusStr)

	place := ctx.PostForm("place")

	AType := ctx.PostForm("type")
	ActType, _ := strconv.Atoi(AType)
	permission := ctx.PostForm("permission")
	joinPermission, _ := strconv.Atoi(permission)
	Stime1 := ctx.PostForm("stime")
	ETime1 := ctx.PostForm("etime")
	STime, _ := time.Parse("2006-01-02 15:04:05", Stime1)
	ETime, _ := time.Parse("2006-01-02 15:04:05", ETime1)

	requirement := ctx.PostForm("requirement")

	content := ctx.PostForm("content")

	File := ctx.PostForm("file")

	//applyCreateID := ctx.PostForm("applyUID")
	MID := ctx.PostForm("managerID")
	managerID, _ := strconv.Atoi(MID)
	DB := common.GetDB()

	// 插入数据表的 SQL 语句
	//query := "INSERT INTO apply_createclub () VALUES (?, ?, ?, ?, ?, ?)"
	//query1 := "INSERT INTO clubs (Cname,Address,Type,Aim,State,Exist,Unit,TeacherName,TeacherPhone,LeaderID,LeaderName,Mailbox,Phone,Introduction) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
	//result1 := DB.Exec(query1, ClubName, Address, ClubType, createAim, club_state, create_state, college, TName, TPhone, LeaderID, LeaderName, ClubMail, ClubPhone, Intro)

	activity := model.Activity{
		ATitle:      AName,
		Cid:         clubid,
		StartTime:   startTime,
		EndTime:     endTime,
		Campus:      Campus,
		Place:       place,
		Type:        ActType,
		Permission:  joinPermission,
		Stime:       STime,
		Etime:       ETime,
		Requirement: mssql.VarChar(requirement),
		Content:     mssql.VarChar(content),
		File:        mssql.VarChar(File),
		UID:         managerID, //活动负责人的id
	}

	//插入activity数据表
	if createErr := DB.Create(&activity).Error; createErr != nil {
		fmt.Println("执行 SQL 语句时发生错误:", createErr)
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		return
	}

	insertAid := activity.Aid
	var applyState = 0
	activityCreate := model.ActivityCreate{
		Aid:       insertAid,
		Uid:       userid,
		ApplyTime: ApplyCreateTime,
		State:     applyState,
	}
	//插入 申请创建活动分表
	if applyErr := DB.Create(&activityCreate).Error; applyErr != nil {
		fmt.Println("执行 SQL 语句时发生错误:", applyErr)
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		return
	}

	DB1 := common.GetDB()

	//得到活动所属的社团信息
	var clubinfo model.Club

	clubResult := DB1.Table("clubs").Where("id = ?", clubid).First(&clubinfo)

	if clubResult.Error != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		log.Println("err:", clubResult.Error)
		return
	}

	applyID := activityCreate.ACid

	//申请总表
	applyinfo := model.Apply{
		Apply_ID:       applyID,
		UserID:         userid,
		Kind:           5,
		Approval_State: 0,
		Title:          ActTitle,
		ApplyTime:      ApplyCreateTime,
		Approver1:      clubinfo.LeaderID, //社团负责人id
		Approver2:      clubinfo.AdminTeacherID,
		//Approver3:, //校团委
	}

	//插入applies申请总表
	if applyErr := DB.Create(&applyinfo).Error; applyErr != nil {
		fmt.Println("执行 SQL 语句时发生错误:", applyErr)
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		return
	} else {
		// response成功提示给前端
		response.Response(ctx, http.StatusOK, 200, nil, "申请创建活动成功")

	}

}
